package com.util;


import org.joda.time.Days;
import org.joda.time.LocalDate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static Date getToday() {
        Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date todayStart = calendar.getTime();
        calendar.add(Calendar.DATE, 1);
//        Date endStart = calendar.getTime();
        return todayStart;
    }


    public static Date parse(String date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.parse(date);
    }

    public static Date getTonight() throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        Date todayEnd=calendar.getTime();
        String time=todayEnd.toString();
        return todayEnd;
    }

    public static Date getMorning() throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date todayEnd=calendar.getTime();
        String time=todayEnd.toString();
        return todayEnd;
    }
    public static  Date parse2(String date) throws ParseException{
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.parse(date);
    }

    /*
     *获取yyyy-MM-dd hh:mm:ss 类型的日期
     */
    public static String dateToStrLong(java.util.Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static int daysBetween(Date one,Date two){
       return Days.daysBetween(
               new LocalDate(one.getTime()),
               new LocalDate(two.getTime())).getDays();
    }

/*
    public static void main(String[] args) throws ParseException {
        Calendar c =Calendar.getInstance();
        c.add(Calendar.MONTH,0);
        c.add(Calendar.DAY_OF_MONTH,-5);
        System.out.println(c);
        Date date = c.getTime();
        System.out.println(c.get(Calendar.DAY_OF_MONTH));
        System.out.println(daysBetween(date,getToday()));
        System.out.println(parse("2016-10-11"));

    }*/
}
